import logging
import unittest
import requests
import time

import utils
from api.login import LoginApi


class TestLogin(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        self.login_api = LoginApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    # 登录成功（用户名和密码输入正确）
    def test01_login_success(self):
        keywords = "13203003592"
        password = "hyc123456"
        # time.sleep(60)
        res_login = self.login_api.login(keywords, password)

        # 断言
        utils.common_assert(self, res_login, 200, 200, "登录成功")

        # 输出日志
        logging.info(f"登录结果为：{res_login.json()}")

    # 登录失败（用户名不存在）
    def test02_login_fail_root_not_exists(self):
        keywords = "13203003599"
        password = "hyc123456"
        res_login = self.login_api.login(keywords, password)

        # 断言
        utils.common_assert(self, res_login, 200, 100, "用户不存在")

        # 输出日志
        logging.info(f"登录结果为：{res_login.json()}")

    # 登录失败（不输入密码)
    def test03_login_fail_password_null(self):
        keywords = "13203003592"
        password = None
        # time.sleep(60)
        res_login = self.login_api.login(keywords, password)

        # 断言
        utils.common_assert(self, res_login, 200, 100, "密码不能为空")

        # 输出日志
        logging.info(f"登录结果为：{res_login.json()}")

    # 登录失败（输入密码错误一次）
    # 登录成功（用户名和密码输入正确）
    def test04_login_fail(self):
        keywords = "13203003592"
        password = "hyc123457"
        password4 = "hyc123456"
        res_login1 = self.login_api.login(keywords, password)
        res_login2 = self.login_api.login(keywords, password)
        res_login3 = self.login_api.login(keywords, password)
        time.sleep(60)
        res_login4 = self.login_api.login(keywords, password4)

        # 断言
        utils.common_assert(self, res_login1, 200, 100, "密码错误1次,达到3次将锁定账户")
        utils.common_assert(self, res_login2, 200, 100, "密码错误2次,达到3次将锁定账户")
        utils.common_assert(self, res_login3, 200, 100, "由于连续输入错误密码达到上限，账号已被锁定，请于1.0分钟后重新登录")
        utils.common_assert(self, res_login4, 200, 200, "登录成功")

        # 输出日志
        logging.info(f"登录结果为：{res_login1.json()}")
        logging.info(f"登录结果为：{res_login2.json()}")
        logging.info(f"登录结果为：{res_login3.json()}")
        logging.info(f"登录结果为：{res_login4.json()}")
